// Copyright (C) 2010  Joan Puigcerver Perez, <joapuipe@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include <Movies.hpp>

#include <iostream>
#include <fstream>
#include <cstring>

#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>
#include <xercesc/util/XMLString.hpp>

#include <SAXMoviesHandler.hpp>

Movies::Movies(std::ostream& logf, ost::Mutex& logm)
  : logfile(logf), log_mutex(logm), num_movies(0)
{
}

Movies::~Movies()
{
  for(MHashMap::iterator it = movies.begin(); it != movies.end(); ++it)
    delete it->second;
  movies.clear();
}

bool Movies::load(Database * database)
{
  /*  try {
    XMLPlatformUtils::Initialize();
  } 
  catch( const XMLException & toCatch ) {
    char* message = XMLString::transcode(toCatch.getMessage());
    print_log_line("Error during initialization! :", logfile, log_mutex);
    print_log_line("Exception message is:", logfile, log_mutex);
    print_log_line(message, logfile, log_mutex);
    XMLString::release(&message);
    return false;
    } */
  
  SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
  
  SAXMoviesHandler handler(database, logfile, log_mutex);
  parser->setContentHandler(&handler);
  parser->setErrorHandler(&handler);
  
  try {
    parser->parse(MOVIES_XML);
  }
  catch (const XMLException& toCatch) {
    char* message = XMLString::transcode(toCatch.getMessage());
    print_log_line("Exception message is:", logfile, log_mutex);
    print_log_line(message, logfile, log_mutex);
    XMLString::release(&message);
    delete parser;
    //XMLPlatformUtils::Terminate();
    return false;
  }
  catch (const SAXParseException& toCatch) {
    char* message = XMLString::transcode(toCatch.getMessage());
    print_log_line("Exception message is:", logfile, log_mutex);
    print_log_line(message, logfile, log_mutex);
    XMLString::release(&message);
    delete parser;
    //XMLPlatformUtils::Terminate();
    return false;
  }
  catch (...) {
    print_log_line("Unexpected exception!", logfile, log_mutex);
    delete parser;
    //XMLPlatformUtils::Terminate();
    return false;
  }
  
  delete parser;
  //XMLPlatformUtils::Terminate();
  return true; 
}

Movie* Movies::add_movie(const std::string& title, const Year year,
			 const Media media, const bool suspended)
{
  Movie * movie = new Movie(num_movies, title, year, media, suspended);
  std::pair<mIter,bool> r = movies.insert(std::pair<const std::string&, Movie*>(movie->title(), movie));
  if ( r.second == true ) {
    num_movies++;
  } else {
    delete movie;
    movie = NULL;
  }
  return movie;
}

void Movies::add_aka(const char* aka, Movie* movie)
{
  
}

Movie* Movies::search(const std::string& title)
{
  mIter it = movies.find(title);
  if ( it != movies.end() ) return it->second;
  return NULL;
}

size_t Movies::size(void) const
{
  return num_movies;
}
